<template>

  <div v-loading="loading" class="AddOrUpdate_main add_container main_container column_two">

    <div class="main_header">
      <div class="padding_container">
        <div class="left" style="color:#606266;margin-left: 8px;">系统设置</div>
        <div class="right" />
      </div>
    </div>
    <el-card class="main_card">
      <div class="main_main">
        <el-form ref="form" :model="formData" :rules="rules" :inline="true">
          <div class="formItem_header">小数设置</div>
          <el-form-item label="小数展示位数" prop="decimalDigits">
            <el-select v-model="formData.decimalDigits">
              <el-option
                v-for="item in decimalDigitsOptions"
                :key="item"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="小数取舍方式" prop="digitsInterception">
            <el-select v-model="formData.digitsInterception">
              <el-option
                v-for="item in digitsInterceptionOptions"
                :key="item.id+''"
                :label="item.label"
                :value="item.id+''"
              />
            </el-select>
          </el-form-item>
          <div class="formItem_header">应收应付</div>
          <el-form-item v-if="isShow" label="是否统计采购成本" class="warehouse_label" prop="productionSettingStatus">
            <el-switch
              v-model="formData.availabilityOfStatisticsOnProcurementCosts"
              active-value="1"
              inactive-value="0"
              active-color="#13ce66"
              inactive-color="#999999"
            />
          </el-form-item>
          <div class="formItem_header">生产管理</div>
          <el-form-item v-if="isShow" label="新增生产计划单时默认生成任务单" class="warehouse_label" prop="productionSettingStatus">
            <el-switch
              v-model="formData.productionSettingStatus"
              active-value="1"
              inactive-value="0"
              active-color="#13ce66"
              inactive-color="#999999"
            />
          </el-form-item>
          <div class="formItem_header">质量管理</div>
          <el-form-item v-if="isShow" label="生产报工完工时默认生成检验单" class="warehouse_label" prop="productionCommencementStatus">
            <el-switch
              v-model="formData.productionCommencementStatus"
              active-value="1"
              inactive-value="0"
              active-color="#13ce66"
              inactive-color="#999999"
            />
          </el-form-item>
          <el-form-item v-if="isShow" label="新增销售退货单时默认生成检验单" class="warehouse_label" prop="saleReturnStatus">
            <el-switch
              v-model="formData.saleReturnStatus"
              active-value="1"
              inactive-value="0"
              active-color="#13ce66"
              inactive-color="#999999"
            />
          </el-form-item>
          <div class="formItem_header">仓库管理</div>
          <!--          <el-form-item v-if="isShow" label="新增销售退货单时默认生成入库单" class="warehouse_label" prop="saleReturnStatus">-->
          <!--            <el-switch-->
          <!--              v-model="formData.saleReturnStatus"-->
          <!--              active-value="1"-->
          <!--              inactive-value="0"-->
          <!--              active-color="#13ce66"-->
          <!--              inactive-color="#999999"-->
          <!--            />-->
          <!--          </el-form-item>-->
          <el-form-item v-if="isShow" label="新增外协退料单时默认生成入库单" class="warehouse_label" prop="outsourceReturnMaterialStatus">
            <el-switch
              v-model="formData.outsourceReturnMaterialStatus"
              active-value="1"
              inactive-value="0"
              active-color="#13ce66"
              inactive-color="#999999"
            />
          </el-form-item>
          <el-form-item v-if="isShow" label="库存清单中是否显示库存为0的数据" class="warehouse_label" prop="warehouseInventoryIsAllShow">
            <el-switch
              v-model="formData.warehouseInventoryIsAllShow"
              active-value="1"
              inactive-value="0"
              active-color="#13ce66"
              inactive-color="#999999"
            />
          </el-form-item>
          <el-form-item v-if="isShow" label="是否开启亮灯拣货功能" class="warehouse_label" prop="warehouseInventoryIsAllShow">
            <el-switch
              v-model="formData.PTL_LIGHT_MAP"
              active-value="1"
              inactive-value="0"
              active-color="#13ce66"
              inactive-color="#999999"
            />
          </el-form-item>
          <el-form-item v-if="isShow" label="生产入库时默认生成批号" class="warehouse_label" prop="tacitlyApproveBatch">
            <el-switch
              v-model="formData.tacitlyApproveBatch"
              active-value="1"
              inactive-value="0"
              active-color="#13ce66"
              inactive-color="#999999"
            />
          </el-form-item>
          <el-form-item v-if="isShow" label="库存清单默认显示物料分类" class="warehouse_label" prop="showMaterialType">
            <el-switch
              v-model="formData.showMaterialType"
              active-value="1"
              inactive-value="0"
              active-color="#13ce66"
              inactive-color="#999999"
            />
          </el-form-item>
          <div class="formItem_header">设备管理</div>

          <el-form-item v-if="isShow" label="维修超时提醒开关" class="warehouse_label" prop="saleReturnStatus">
            <el-switch
              v-model="formData.deviceRepairOvertimeStatus"
              active-value="1"
              inactive-value="0"
              active-color="#13ce66"
              inactive-color="#999999"
            />
          </el-form-item>
          <el-form-item label="维修超时提醒天数" prop="deviceRepairOvertime">
            <el-input v-model="formData.deviceRepairOvertime" placeholder="暂无" :disabled="formData.deviceRepairOvertimeStatus == 0?true:false" />
          </el-form-item>
          <el-form-item v-if="isShow" label="点检异常时默认生成报修单" class="warehouse_label" prop="deviceCheckStatus">
            <el-switch
              v-model="formData.deviceCheckStatus"
              active-value="1"
              inactive-value="0"
              active-color="#13ce66"
              inactive-color="#999999"
            />
          </el-form-item>
          <el-form-item label="维修人员配置">
            <el-select v-model="formData.MaintenanceRole" clearable>
              <el-option
                v-for="item in roleOptions"
                :key="item.roleId"
                :label="item.roleName"
                :value="item.roleId"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <div class="dangerSetting">
          <div class="dangerSetting_name">
            <div class="dangerSetting_name1">初始化数据</div>
            <div class="dangerSetting_name2">初始化数据将删除所有业务数据，初始化数据后数据无法恢复。请谨慎操作！</div>
          </div>
          <div class="dangerSetting_operate">
            <el-button @click.stop="initCompanyData">初始化</el-button>
          </div>
        </div>
      </div>
      <div class="main_bottom">
        <el-button @click.stop="onSubmit">保存</el-button>
      </div>

    </el-card>

  </div>
</template>

<script>
import { list } from '@/api/system/role'
import { addOrUpdateSystemSet as addOrUpdate, getSystemSet as getInfo } from '@/api/common/systemSet'
import { mapGetters } from 'vuex'
import InitCompanyData from './initCompanyData'
export default {
  name: 'SystemSet',
  computed: {
    ...mapGetters([
      'userId', 'userName', 'companyId'
    ])
  },
  data() {
    const deviceRepairOvertime = (rule, value, callback) => {
      if (value) {
        if (!this.SETTINGS.reg_integer.test(value)) {
          callback(new Error('维修超时天数提醒天数只能为整数'))
        } else if (value == 0) {
          callback(new Error('维修超时天数提醒天数不能为0'))
        } else {
          callback()
        }
      } else {
        callback()
      }
    }
    return {
      loading: true,
      isShow: false,
      formData: {
        MaintenanceRole: '',
        decimalDigits: '2',
        digitsInterception: '4',
        saleReturnStatus: '1',
        warehouseInventoryIsAllShow: '0',
        outsourceReturnMaterialStatus: '1',
        productionCommencementStatus: '1',
        availabilityOfStatisticsOnProcurementCosts: '0',
        productionSettingStatus: '1',
        deviceCheckStatus: '0',
        deviceRepairOvertimeStatus: '0',
        PTL_LIGHT_MAP: '0',
        tacitlyApproveBatch: '0',
        showMaterialType: '1',
        deviceRepairOvertime: 1
      },
      pageData: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'updateTime',
        isAsc: 'desc',
        status: '1'
      },
      roleOptions: [],
      decimalDigitsOptions: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10'],
      digitsInterceptionOptions: [
        { id: '4', label: '四舍五入' },
        { id: '2', label: '向上取整' },
        { id: '3', label: '向下取整' }
      ],
      rules: {
        decimalDigits: [
          { required: true, message: '小数展示位数不能为空', trigger: 'blur' }
        ],
        digitsInterception: [
          { required: true, message: '小数取舍方式不能为空', trigger: 'blur' }
        ],
        saleReturnStatus: [
          { required: true, message: '销售退货单时默认生成入库单不能为空', trigger: 'blur' }
        ],
        outsourceReturnMaterialStatus: [
          { required: true, message: '外协退料单时默认生成入库单不能为空', trigger: 'blur' }
        ],
        productionCommencementStatus: [
          { required: true, message: '生产报工完工时默认生成检验单不能为空', trigger: 'blur' }
        ],
        deviceCheckStatus: [
          { required: true, message: '点检异常时默认生成报修单不能为空', trigger: 'blur' }
        ],
        productionSettingStatus: [
          { required: true, message: '新增生产计划单时默认生成任务单不能为空', trigger: 'blur' }
        ],
        warehouseInventoryIsAllShow: [
          { required: true, message: '新增生产计划单时默认生成任务单不能为空', trigger: 'change' }
        ],
        deviceRepairOvertime: [
          { required: true, validator: deviceRepairOvertime, trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getRoleList()
  },
  mounted() {
    this.getById()
  },
  methods: {
    // 获取角色列表
    getRoleList() {
      list(this.pageData).then(res => {
        this.roleOptions = res.data
        this.roleOptions.forEach((item) => {
          item.roleId = item.roleId.toString()
        })
        console.log(this.roleOptions)
      })
    },
    getById() {
      getInfo(this.companyId).then(res => {
        Object.keys(this.formData).forEach(key => { // 给右边数据赋值
          this.formData[key] = res.data[key] || this.formData[key]
        })
        this.isShow = true
        this.loading = false
      })
    },
    onSubmit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (!this.formData.deviceRepairOvertime && this.formData.deviceRepairOvertimeStatus == 1) {
            this.$message.warning('维修超时天数提醒天数不能为空')
            return
          }
          addOrUpdate(this.formData).then(res => {
            this.$message({
              message: res.msg,
              type: 'success'
            })
            this.getSystemSet()
          })
        }
      })
    },

    getSystemSet() {
      // 获取系统设置数据
      getInfo(this.companyId).then(res => {
        // 获取系统设置中的 小数展示位数
        const settings_decimalDigits = res.data.decimalDigits || 2
        // 通过小数展示位数，计算数字输入框需要的宽度
        this.SETTINGS.settings_decimalDigits_length = settings_decimalDigits * 20 + 100
      })
    },
    initCompanyData() { // 初始化数据
      this.$layer.iframe({// 弹框
        content: {
          content: InitCompanyData, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
          }// props
        },
        area: ['440px', '300px'],
        title: '初始化数据', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    }
  }
}
</script>

<style lang="less" scoped>
/deep/ .el-form-item__label {
  width: 170px !important;
}
/deep/.warehouse_label {
  .el-form-item__label {
    width: 580px !important;
  }
}

.main_card{
  width: 880px;
  margin-left: 20px;
}

/deep/ .el-card{
  height: 100%;
  margin-bottom: 20px;

  .el-card__body{
    padding: 0px;
    height: 100%;
  }
}

.main_main{
  overflow: scroll !important;
  margin: 0;height: calc(100% - 70px);
  /deep/ .el-form{
    width: 100%;
  }
}
.main_bottom{
  background-color: unset;
  padding-right: 28px !important;
}
.dangerSetting{
  margin: 0 20px;
  display: flex;align-items: center;
  justify-content: space-between;
  padding-top: 20px;
  .dangerSetting_name{
    .dangerSetting_name1{
      font-size: 18px;
    }
    .dangerSetting_name2{
      margin-top: 5px;
      font-size: 14px;
      color: #666;
    }
  }
  .dangerSetting_operate{
    width: 100px;
    .el-button{
      background-color: rgb(222, 71, 83);
    }

    .el-button:hover{
      background-color:rgb(242, 109, 120);
    }
  }
}
</style>
